package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.ReportLqBmjrjlbBsInfo;
import com.ruoyi.report_lq.domain.bo.ReportLqBmjrjlbBsInfoBo;
import com.ruoyi.report_lq.domain.vo.ReportLqBmjrjlbBsInfoVo;
import com.ruoyi.report_lq.mapper.ReportLqBmjrjlbBsInfoMapper;
import com.ruoyi.report_lq.service.IReportLqBmjrjlbBsInfoService;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportLqBmjrjlbBsInfoServiceImpl implements IReportLqBmjrjlbBsInfoService {

    private final ReportLqBmjrjlbBsInfoMapper baseMapper;

    private final ReportLqBmjrsyjcjlbBa1ServiceImpl ba1Service;
    private final ReportLqBmjrsyjcjlbBa2ServiceImpl ba2Service;
    private final ReportLqBmjrsyjcjlbBa3ServiceImpl ba3Service;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    @FIllBasicInfo
    public ReportLqBmjrjlbBsInfoVo queryById(Long excelId){
        ReportLqBmjrjlbBsInfoVo inVo = baseMapper.selectVoById(excelId);
        inVo.setBm1List(ba1Service.queryByInfoId(inVo.getExcelId()));
        inVo.setBm2List(ba2Service.queryByInfoId(inVo.getExcelId()));
        inVo.setBm3List(ba3Service.queryByInfoId(inVo.getExcelId()));
        return inVo;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportLqBmjrjlbBsInfoVo> queryPageList(ReportLqBmjrjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqBmjrjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqBmjrjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportLqBmjrjlbBsInfoVo> queryList(ReportLqBmjrjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqBmjrjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqBmjrjlbBsInfo> buildQueryWrapper(ReportLqBmjrjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqBmjrjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqBmjrjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqBmjrjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqBmjrjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqBmjrjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqBmjrjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqBmjrjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqBmjrjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqBmjrjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqBmjrjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqBmjrjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqBmjrjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqBmjrjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqBmjrjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqBmjrjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqBmjrjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqBmjrjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqBmjrjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqcd()), ReportLqBmjrjlbBsInfo::getLiqcd, bo.getLiqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqzljbh()), ReportLqBmjrjlbBsInfo::getLiqzljbh, bo.getLiqzljbh());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportLqBmjrjlbBsInfoBo bo) {
        ReportLqBmjrjlbBsInfo add = BeanUtil.toBean(bo, ReportLqBmjrjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportLqBmjrjlbBsInfoBo bo) {
        boolean flag;
        ReportLqBmjrjlbBsInfo update = BeanUtil.toBean(bo, ReportLqBmjrjlbBsInfo.class);
        validEntityBeforeSave(update);
        flag = baseMapper.updateById(update) > 0;
        //TODO 子表1
        ba1Service.insertOrUpdateByBmDetail1Bo(bo.getBm1List(), update.getExcelId());
        //TODO 子表2
        ba2Service.insertOrUpdateByBmDetail2Bo(bo.getBm2List(), update.getExcelId());
        //TODO 子表3
        ba3Service.insertOrUpdateByBmDetail3Bo(bo.getBm3List(), update.getExcelId());
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqBmjrjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
